package com.itboye.sunsun.support;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

import android.app.Activity;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class BaseFragment extends Fragment {


//	@Override
//	public View onCreateView(LayoutInflater inflater,
//			@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
//		// TODO Auto-generated method stub
//		View v= super.onCreateView(inflater, container, savedInstanceState);
//		
//		smartInject(v);
//		return  v;
//	}
//	

	protected void smartInject(View view) {  
        Class<? extends Fragment> clz= getClass();   
        Field[] fs=clz.getDeclaredFields();  
        Resources res = getResources();  
        String packageName = getActivity().getPackageName();  
        for (Field field : fs) {  
        	if (!View.class.isAssignableFrom(field.getType())) {
				continue;
			}
            int viewId = res.getIdentifier(field.getName(), "id", packageName);  
            if (viewId==0)   
                continue;  
            field.setAccessible(true);  
            try {  
                View v=view.findViewById(viewId);  
                field.set(this,v);  
                Class<?> c=field.getType();  
                Method m=c.getMethod("setOnClickListener", android.view.View.OnClickListener.class);  
                m.invoke(v, this);  
            } catch (Throwable e) {  
            }  
            field.setAccessible(false);  
              
        }  
          
    } 
	
}
